#!/bin/env bash
# 运行 cloudstack 之前，检测环境，准备环境
python /run.py
echo

rpcbind
cloudstack-setup-management --tomcat7 --no-start

# 运行 cloudstack-management
set -a
. /etc/default/cloudstack-management
set +a
export JAVA_HOME=$( readlink -f $( which java ) | sed s:bin/.*$::)


# RUN
/usr/bin/jsvc -debug -user "${TOMCAT_USER}" -cp "$CLASSPATH" \
    -outfile /var/log/jsvc.log  -errfile /var/log/cloudstack/management/jsvc.log \
    -pidfile "${CATALINA_PID}" ${JAVA_OPTS} \
    -Dcatalina.base="${CATALINA_BASE}" -Dcatalina.home="${CATALINA_HOME}" -Djava.io.tmpdir="${CATALINA_TMPDIR}" "${BOOTSTRAP_CLASS}"

function stop_cloudstack() {
    echo "stoping cloudstack"
    /usr/bin/jsvc -cp "$CLASSPATH" -pidfile "$CATALINA_PID" -stop "$BOOTSTRAP_CLASS"
}

# 清理
trap "stop_cloudstack" HUP INT QUIT TERM

while [ true ]; do
    if [ -f /var/log/cloudstack/management/management-server.log ]; then
        break;
    fi
    echo 'waiting for log is out!'
    sleep 3
done

tail -f -n100 /var/log/cloudstack/management/management-server.log